<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    :okButtonProps="{ props: {disabled: disableSubmit} }"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <a-form :form="form">
        <a-row>

          <a-form-item label="账单日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <j-date placeholder="请选择账单日期"
                    :disabledDate="disabledDate"
                    v-decorator="['date', validatorRules.date]" :trigger-change="true"
                    style="width: 100%"/>
          </a-form-item>

          <a-form-item v-is-group label="所属公司" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <j-company-select-tag placeholder="请选择所属公司" v-decorator="[ 'companyId', validatorRules.companyId]"
                                  :trigger-change="true" />
          </a-form-item>

        </a-row>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

import pick from 'lodash.pick'
import JDate from '@/components/jeecg/JDate'
import JUpload from '@/components/jeecg/JUpload'
import JDictSelectTag from '@/components/dict/JDictSelectTag'
import { downloadStatementAccount } from '@/api/statementAccount'
import JCompanySelectTag from '@comp/jeecg/JCompanySelectTag'
import { IS_GROUP } from '@/store/mutation-types'
import moment from 'moment'

export default {
  name: 'StatementAccountOperationModal',
  components: {
    JDate,
    JUpload,
    JCompanySelectTag,
    JDictSelectTag
  },
  data () {
    return {
      form: this.$form.createForm(this),
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      labelCol2: {
        xs: { span: 24 },
        sm: { span: 3 }
      },
      wrapperCol2: {
        xs: { span: 24 },
        sm: { span: 20 }
      },
      width: 700,
      disableSubmit: false,
      confirmLoading: false,
      visible: false,
      title: '',
      model: {},
      validatorRules: {
        date: {
          rules: [
            { required: true, message: '请选择账单日期!' },
          ]
        },
      }
    }
  },
  methods: {
    disabledDate(current) {
      return current > moment().subtract(1, 'days').endOf('day')
    },
    show () {
      this.confirmLoading = false
      this.title = '手动下载对账单'
      this.form.resetFields()
      this.visible = true
    },
    handleOk () {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          // let formData = Object.assign(this.model, values)
          let formData = values
          downloadStatementAccount(formData).then((res) => {
            if (res.success) {
              this.$message.success(res.message)
              this.$emit('ok')
              this.close()
            } else {
              this.$message.warning(res.message)
            }
          }).finally(() => {
            this.confirmLoading = false
          })
        }

      })
    },
    handleCancel () {
      this.close()
    },
    close () {
      this.$emit('close')
      this.visible = false
    }
  }
}
</script>

<style scoped>
</style>